﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BMSQ.Utility;

namespace BMSQ.Admin
{
    public class Common
    {
        public const int MAXIMAGESIZE = 1024 * 200;//APP端图片不能大于500K

        public static string GoodsImageFolder()
        {
            return ConfigHelper.GetConfigString("GoodsImageFolder");
        }

        public static string VendorImageFolder()
        {
            return ConfigHelper.GetConfigString("VendorImageFolder");
        }

        public static string APPImageExtension()
        {
            return ConfigHelper.GetConfigString("APPImageExtension");
        }

        public static bool CheckAPPImageExtension(string extension)
        {
            if (extension.StartsWith(".")) extension = extension.Substring(1, extension.Length - 1);
            string[] exts = APPImageExtension().ToUpper().Split(',');
            if (exts.Contains(extension.ToUpper()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Vincent.Permission.Model.Ex.UserInfo UserInfo
        {
            get
            {
                string CacheKey = "QDT-UserInfo";
                object cacheValue = DataCache.GetCache(CacheKey);

                if (cacheValue == null && HttpContext.Current.Session["UserInfo"] == null)
                {
                    return null;
                }
                else if (cacheValue == null)
                {
                    try
                    {
                        cacheValue = HttpContext.Current.Session["UserInfo"];
                        DataCache.SetCache(CacheKey, cacheValue, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                        return (Vincent.Permission.Model.Ex.UserInfo)HttpContext.Current.Session["UserInfo"];
                    }
                    catch
                    {
                        return null;
                    }
                }
                else
                {
                    HttpContext.Current.Session["UserInfo"] = DataCache.GetCache(CacheKey);
                    return (Vincent.Permission.Model.Ex.UserInfo)HttpContext.Current.Session["UserInfo"];
                }
            }
            set
            {
                HttpContext.Current.Session["UserInfo"] = value;
                string CacheKey = "QDT-UserInfo";
                DataCache.SetCache(CacheKey, value, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
            }
        }

        public static string SelectedNodeID
        {
            get
            {
                string CacheKey = "QDT-SelectedNodeID";
                object cacheValue = DataCache.GetCache(CacheKey);

                if (cacheValue == null && HttpContext.Current.Session["SelectedNodeID"] == null)
                {
                    return null;
                }
                else if (cacheValue == null)
                {
                    try
                    {
                        cacheValue = HttpContext.Current.Session["SelectedNodeID"];
                        DataCache.SetCache(CacheKey, cacheValue, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                        return HttpContext.Current.Session["SelectedNodeID"].ToString();
                    }
                    catch
                    {
                        return null;
                    }
                }
                else
                {
                    HttpContext.Current.Session["SelectedNodeID"] = DataCache.GetCache(CacheKey);
                    return HttpContext.Current.Session["SelectedNodeID"].ToString();
                }
            }
            set
            {
                HttpContext.Current.Session["SelectedNodeID"] = value;
                string CacheKey = "QDT-SelectedNodeID";
                DataCache.SetCache(CacheKey, value, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
            }
        }

        public static string VirtualPath()
        {
            return ConfigHelper.GetConfigString("VirtualPath");
        }

        public static string WebURL()
        {
            return ConfigHelper.GetConfigString("WebURL");
        }
    }
}